#include "GnssControl.h"
#include <pthread.h>
#include "Log.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/system_properties.h>


int carplaySessionConnected = 0;
pthread_t 		m_GnssControlTid;
pthread_t *		m_GnssControlTidPtr;

void (*pRequestStartLocation)( );
volatile int keepGnssRunning = 1;

void *gnssControlThread(void *arg) {
	keepGnssRunning = 1;
	while ( keepGnssRunning ){

		if( carplaySessionConnected == 1)
		{
			sleep( 15 );
			if( pRequestStartLocation != NULL )
			{
				pRequestStartLocation();
			}
			keepGnssRunning = 0;
			break;
		}
		usleep(700*1000);
	}

	return (NULL);
}

void RegisterRequestStartLocation(void (*pFunc)())
{
	pRequestStartLocation = pFunc;
	int ret;
	if ((ret = pthread_create(&m_GnssControlTid, NULL, gnssControlThread, NULL))
			!= 0) {
		LOGD("observe carplay session thread create failed!\n");
	} else {
		m_GnssControlTidPtr = &m_GnssControlTid;
		LOGD("observe carplay session thread create successed!\n");
	}
}

void OnCarPlaySessionConnected( )
{
	carplaySessionConnected = 1;
}


void OnCarPlaySessionDisconnected( )
{
	carplaySessionConnected = 0;
}




